package com.smartrodin.springbootrabbitmq.service;

import com.alibaba.fastjson.JSON;
import com.smartrodin.springbootrabbitmq.entity.MqSendFileLog;
import com.smartrodin.springbootrabbitmq.mapper.MqSendFileLogMapper;
import com.smartrodin.springbootrabbitmq.util.Constant;
import com.smartrodin.springbootrabbitmq.util.IDGenerator;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author arnold_nie
 * @Description: mq 发送失败记录表 服务实现类
 * @date 2020-07-09
 * @version: V1.0
 */
@Component
@Service
public class MqSendFileLogServiceImpl implements IMqSendFileLogService {

    @Autowired
    private MqSendFileLogMapper mqSendFileLogMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void sendMsgAndRecord(String msg, String routingKey, String exchange, Integer msgSendType) {
        String correlationId = IDGenerator.getInstance().getId();
        CorrelationData correlation = new CorrelationData( correlationId );
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setCorrelationId( correlationId );
            messageProperties.setHeader( Constant.MSG_SEND_TYPE, msgSendType );
            return message;
        };
        MqSendFileLog fileLog = new MqSendFileLog();
        fileLog.setExchange( exchange );
        fileLog.setMsg( msg );
        fileLog.setMsgSendType( msgSendType );
        fileLog.setRoutingKey( routingKey );
        redisTemplate.opsForValue().set( Constant.MESSAGE_SEND_MQ + correlationId, JSON.toJSONString( fileLog ) );
        rabbitTemplate.convertAndSend( exchange, routingKey, msg, messagePostProcessor, correlation );
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void test() {
        MqSendFileLog fileLog = new MqSendFileLog();
        fileLog.setCreateTime("2020-11-30" );
        fileLog.setErrorMsg( "MqSendFileLogServiceImpl类" );
        mqSendFileLogMapper.insert( fileLog );
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void test2() {
        MqSendFileLog fileLog = new MqSendFileLog();
        fileLog.setCreateTime("2020-11-30" );
        fileLog.setErrorMsg( "MqSendFileLogServiceImpl---test2--类" );
        mqSendFileLogMapper.insert( fileLog );
    }

}
